<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_public extends CI_Model {

    function __construct() {
        //parent::__construct();
        $this->load->database();
    }

    function getRowInfo($table, $where, $columm = '') {
        return $this->db->select($columm ? $columm : '*')->get_where($table, $where)->row_array();
    }

    function getAllInfo($table, $where, $columm = '') {
        return $this->db->select($columm ? $columm : '*')->get_where($table, $where)->result_array();
    }

    function get_list_count($data) {
        $this->db->from($data['from']);
        if (isset($data['where'])) {
            foreach ($data['where'] as $key => $value) {
                if($key == 'like'){
                    $this->db->like($value['column'],$value['content'],$value['type']);
                    unset($data['where'][$key]);
                }
            }
            $this->db->where($data['where']);
        }
        if (isset($data['join'])) {
            foreach ($data['join'] as $k => $v) {
                $this->db->join($k, $v);
            }
        }
        $this->db->select('count(1) as cnt');
        $query = $this->db->get();
        return $query->row_array();
    }

    function get_list($data) {
     
        $this->db->from($data['from']);
        if (isset($data['limit'])) {
            $this->db->limit($data['limit'], $data['offset']);
        }
        if (isset($data['where'])) {
            if(is_array($data['where'])){
                foreach ($data['where'] as $key => $value) {
                    if($key == 'like'){
                        $this->db->like($value['column'],$value['content'],$value['type']);
                        unset($data['where'][$key]);
                    }
                }
            }
            $this->db->where($data['where']);
        }
        if (isset($data['join'])) {
            foreach ($data['join'] as $k => $v) {
                $this->db->join($k, $v);
            }
        }
        if (isset($data['order'])) {
            $this->db->order_by($data['order']);
        }
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }

        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_list_page($data, $page, $limit) {
        $page_list = $this->get_list_count($data);
        if ($page < 1) {
            $page = 1;
        }
        $page_list['page'] = $page;
        if ($limit < 0 || $limit > 100) {
            $limit = 10;
        }
        $page_list['limit'] = $limit;
        $list = array();
        if ($page_list['cnt'] > 0) {
            $start = ($page - 1) * $limit;
            $data['limit'] = $limit;
            $data['offset'] = $start;
            $list = $this->get_list($data);
        }
        $result = array(
            'page' => $page_list,
            'list' => $list,
        );
        return $result;
    }

    function getPageAllInfo($table, $where, $limit, $offset, $columm = '') {
        $this->db->select($columm ? $columm : '*');
        $this->db->where($where);
        $this->db->from($table);
        $this->db->limit($limit, $offset);
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function addRowInfo($table, $data) {
        $this->db->insert($table, $data);
        $this->db->insert_id();
    }

    function addAllInfo($table, $data) {
        $this->db->insert_batch($table, $data);
        return $this->db->insert_id();
    }

    function editInfo($where, $data, $table) {
        $this->db->where($where);
        return $this->db->update($table, $data);
    }

    function delInfo($table, $where) {
        return $this->db->delete($table, $where);
    }

    public function write_logs($content, $file) {
        $arrayName = array(
            'logcontent' => $content,
            'scriptfile' => $file,
            'ipaddress' => $this->get_client_ip(),
            'addtime' => date('Y-m-d H:i:s'),
            'adminid' => $this->session->userdata['user_id'],
        );
        return $this->addRowInfo('adminlog', $arrayName); //->insert('adminlog',$arrayName);echo $this->db->last_query();
    }

    public function get_client_ip() {//获取IP
        if (getenv('HTTP_CLIENT_IP')) {
            $onlineip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR')) {
            $onlineip = getenv('REMOTE_ADDR');
        } else {
            $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
        }
        return $onlineip;
    }

}
